package com.liesheng.haylou.ui.Register;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.liesheng.haylou.R;
import com.liesheng.haylou.base.BaseActivity;
import com.liesheng.haylou.base.BaseFunActivity;
import com.liesheng.haylou.base.BaseVm;
import com.liesheng.haylou.bean.RegSmsCode;
import com.liesheng.haylou.bean.RegisterBean;
import com.liesheng.haylou.databinding.ActivityRegisterBinding;
import com.liesheng.haylou.net.HttpCallback;
import com.liesheng.haylou.net.HttpRequest;
import com.liesheng.haylou.ui.Login.LoginActivity;
import com.liesheng.haylou.ui.main.MainActivity;
import com.liesheng.haylou.utils.FormatUtil;
import com.liesheng.haylou.utils.ToastUtil;
import com.liesheng.haylou.utils.Utils;
import com.sahooz.library.Country;
import com.sahooz.library.PickActivity;

import java.util.HashMap;
import java.util.Map;


/**
 * Date:${DATA}
 * Author ${ZhangZhiQiang}
 * Description
 */
public class RegisterActivity extends BaseActivity<ActivityRegisterBinding, BaseVm> {
    private InputMethodManager imm;
    String countryCode;
    String countryName;
    String userPhone;
    String tpy;
    String password;
    String smsCode;
    Intent intent;
    Country country;

    @Override
    protected ActivityRegisterBinding getmBinding() {
        mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_register, null, false);
        //隐藏掉整个ActionBar
        return mBinding;
    }

    @Override
    protected void initData() {
        showHeadLayout(false);
        mBinding.tvCountry.setVisibility(View.VISIBLE);
        mBinding.btnMSM.setText("获取验证码");

    }

    @Override
    protected BaseVm getViewModel() {
        return null;
    }

    @Override
    public void onClickView(View view) {
        switch (view.getId()) {
            case R.id.ivSpiner:
                intent = new Intent(this, PickActivity.class);
                startActivityForResult(intent, 111);
                break;
            case R.id.etRegisterName:
                String phone = mBinding.etRegisterName.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showToast("请输入11位手机号码");
                    return;
                }
                if (!Utils.isMobile(phone)) {
                    ToastUtil.showToast("请输入正确的手机号码");
                    return;
                }
                // 先影藏输入法
                hideSoftKeyBoard();
                break;
            case R.id.bt_regist:
                register();
                break;
            case R.id.lltvNorLog:
                LoginActivity.startBy(this);
                break;
            case R.id.btnMSM:
                getSmS();
                break;
            case R.id.llTiaoG:
                MainActivity.startBy(this);
                break;
            default:
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestcode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestcode, resultCode, data);
        if (requestcode == 111 && resultCode == Activity.RESULT_OK) {
            country = Country.fromJson(data.getStringExtra("country"));
            if (country.flag != 0)
                mBinding.tvCountry.setText("+" + country.code);
        }
    }

    /**
     * 获取验证码
     */
    private void getSmS() {
        if (TextUtils.isEmpty(countryCode) && country != null) {
            countryCode = "" + country.code;
        }
        userPhone = mBinding.etRegisterName.getText().toString();
        tpy = "REG";
        if (TextUtils.isEmpty(userPhone)) {
            ToastUtil.showToast("请输入手机号码！");
            return;
        }
        if (TextUtils.isEmpty(tpy)) {
            ToastUtil.showToast("REG！");
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("countryCode", countryCode);
        map.put("userPhone", userPhone);
        map.put("type", tpy);
        requestHttp(buildHttpService().getSmsCode(countryCode, userPhone, tpy), new HttpCallback<RegSmsCode>() {
            @Override
            public void onNext(RegSmsCode regSmsCode) {
                if (null != regSmsCode && regSmsCode.getCode() == 200){
                    ToastUtil.showToast("请查看你手机短信验证码！");
                }else {
                    ToastUtil.showToast("获取验证码失败！");
                }
            }
        });

    }

    /**
     * 注册
     */
    private void register() {
        hideSoftKeyBoard();
        userPhone = mBinding.etRegisterName.getText().toString();
        password = mBinding.etRegisterPsd.getText().toString();
        smsCode = mBinding.etRegisterVerif.getText().toString();
        if (TextUtils.isEmpty(userPhone)) {
            ToastUtil.showToast("请输入正确的手机号码！");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtil.showToast("请输入6-16位任意字符密码！");
            return;
        }
        if (TextUtils.isEmpty(smsCode)) {
            ToastUtil.showToast("请输入验证码！");
            return;
        }
        if (TextUtils.isEmpty(countryCode) && country != null) {
            countryCode = "" + country.code;
        }
        if (TextUtils.isEmpty(countryName) && country != null) {
            countryName = "" + country.name;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("countryCode", countryCode);
        map.put("countryName", countryName);
        map.put("userPhone", userPhone);
        map.put("password", password);
        map.put("smsCode", smsCode);
        requestHttp(buildHttpService().register(HttpRequest.getBody(map)), new HttpCallback<RegisterBean>() {
            @Override
            public void onNext(RegisterBean registerBean) {
            }
        });
    }

    public static void startBy(BaseFunActivity activity) {
        Intent intent = new Intent(activity, RegisterActivity.class);
        activity.startActivity(intent);
    }


    public void hideSoftKeyBoard() {
        View localView = getCurrentFocus();
        if (this.imm == null) {
            this.imm = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
        }
        if ((localView != null) && (this.imm != null)) {
            this.imm.hideSoftInputFromWindow(localView.getWindowToken(), 2);
        }
    }
}
